<!-- user表单弹窗 -->
<form id="user-form" lay-filter="user-form" class="layui-form model-form">
    <input name="userId" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">账号</label>
        <div class="layui-input-block">
            <input name="username" placeholder="请输入账号" type="text" class="layui-input" maxlength="20"  id="username"
                   lay-verify="required" required/>
        </div>
    </div>
    <!-- <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input name="nickname" placeholder="请输入用户名" type="text" class="layui-input" maxlength="20"
                   lay-verify="required" required/>
        </div>
    </div> -->
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input name="mobile" placeholder="请输入手机号" type="text" class="layui-input" lay-verify="required|mobile"
                   required/>
        </div>
    </div>
    <!--  <div class="layui-form-item">
         <label class="layui-form-label">性别</label>
         <div class="layui-input-block">
             <input type="radio" name="sex" value="0" title="男" checked/>
             <input type="radio" name="sex" value="1" title="女"/>
         </div>
     </div> -->
    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-block">
            <select name="roleId" xm-select="roleId">
            </select>
        </div>
    </div>
    <div class="layui-form-item model-form-footer">
        <!--   <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button> -->
        <button class="layui-btn" lay-filter="user-form-submit" lay-submit>保存</button>
    </div>
</form>

<script>
    layui.use(['layer', 'admin', 'form', 'formSelects'], function () {
        var layer = layui.layer;
        var admin = layui.admin;
        var form = layui.form;
        var formSelects = layui.formSelects;

        form.render('radio');
        var user = admin.getTempData('t_user');
        // var business = admin.getTempData('t_business');
        var editUserindex=admin.getTempData('editUserindex');
        var editindex=null;
        var isExist=true;//用户名是否存在
        // 获取所有角色
        layer.load(2);
        admin.req('sys/sysrole/allRoles', {}, function (data) {
            layer.closeAll('loading');
            if (0 == data.code) {
                // 渲染多选下拉框
                var roleSelectData = new Array();
                if(!data.data)return;
                for (var i = 0; i < data.data.length; i++) {
                    roleSelectData.push({name: data.data[i].roleName, value: data.data[i].roleId});
                }
                formSelects.data('roleId', 'local', {arr: roleSelectData});

                // 回显user数据

                console.log(user);
                $('#user-form').attr('method', 'post');
                if (user.method=="update") {
                    form.val('user-form', user);
                    $('#user-form').attr('method', 'post');
                    admin.req('sys/sysrole/allRoles', {userId:user.userId}, function (e) {
                        console.log(e)
                        var rds = new Array();
                        if (user.roles)
                            for (var i = 0; i < user.roles.length; i++) {
                                rds.push(user.roles[i].roleId);
                            }
                        formSelects.value('roleId', rds);
                    })
                }
            } else {
                layer.msg('获取角色失败', {icon: 2, time: 500});
            }
        }, 'GET');
        //检查用户名是否已存在
        $("#username").blur(function(e){
            //console.log(e.currentTarget.value)
            var name=e.currentTarget.value;
            var node = this;
            if(user.method=="save"){
                admin.req('sys/sysuser/checkUsername?username='+name,  { } , function (data) {
                    if(data.code!=0){
                        layer.msg(data.msg, {icon: 2, time: 500});
                        node.focus();
                        isExist=true;
                    }else{
                        isExist=false;
                    }
                }, "post")
            }
        })

        // 表单提交事件
        form.on('submit(user-form-submit)', function (data) {
            layer.load(2);
            if(user.businessId){
                data.field.businessId=user.businessId;
            }
            $('#user-form').attr('method', 'POST');
            if(user.method=="save"&&isExist)return false;
            data.field.roleIds=data.field.roleId;
            admin.req('sys/sysuser/'+user.method, JSON.stringify(data.field), function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
                    // admin.finishPopupCenter();
                    layer.close(editUserindex);
                    admin.table.reload('user-table', {});
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                }
            }, "POST");
            return false;
        });
    });
</script>